#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class MaterialProvider : CY.GFive.Core.Providers.DALProvider.IMaterialProvider
	{
		#region Sql
		private static readonly string SqlSelectMaterial = "SELECT [Id], [Code],[InPosition],[UnitCode],[BookDate],[PreArriveDate],[ArriveTimeDate],[AcceptResult],[Num] FROM [Material] WHERE  [Id]=@Id";

		private static readonly string SqlInsertMaterial = "INSERT INTO [Material]([Code],[InPosition],[UnitCode],[BookDate],[PreArriveDate],[ArriveTimeDate],[AcceptResult],[Num]) values(@Code,@InPosition,@UnitCode,@BookDate,@PreArriveDate,@ArriveTimeDate,@AcceptResult,@Num);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateMaterial = "UPDATE [Material] SET [Code] = @Code,[InPosition] = @InPosition,[UnitCode] = @UnitCode,[BookDate] = @BookDate,[PreArriveDate] = @PreArriveDate,[ArriveTimeDate] = @ArriveTimeDate,[AcceptResult] = @AcceptResult,[Num] = @Num WHERE  [Id]=@Id";

		private static readonly string SqlDeleteMaterial = "DELETE [Material] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllMaterial = "SELECT [Id], [Code],[InPosition],[UnitCode],[BookDate],[PreArriveDate],[ArriveTimeDate],[AcceptResult],[Num] FROM [Material]";
		#endregion

		#region IMaterialProvider Members
		public Core.Business.Material Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectMaterial);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Material material = new Core.Business.Material();

				if (!reader.IsDBNull(0)) material.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) material.Code = reader.GetString(1);
				if (!reader.IsDBNull(2)) material.InPosition = reader.GetBoolean(2);
				if (!reader.IsDBNull(3)) material.UnitCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) material.BookDate = reader.GetDateTime(4);
				if (!reader.IsDBNull(5)) material.PreArriveDate = reader.GetDateTime(5);
				if (!reader.IsDBNull(6)) material.ArriveTimeDate = reader.GetDateTime(6);
				if (!reader.IsDBNull(7)) material.AcceptResult = reader.GetString(7);
				if (!reader.IsDBNull(8)) material.Num = reader.GetInt32(8);

				return material;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Material material)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, material.Code);
			sql.AddParameter("@InPosition", SqlDbType.Bit, material.InPosition);
			sql.AddParameter("@UnitCode", SqlDbType.NVarChar, material.UnitCode);
			sql.AddParameter("@BookDate", SqlDbType.DateTime, material.BookDate);
			sql.AddParameter("@PreArriveDate", SqlDbType.DateTime, material.PreArriveDate);
			sql.AddParameter("@ArriveTimeDate", SqlDbType.DateTime, material.ArriveTimeDate);
			sql.AddParameter("@AcceptResult", SqlDbType.NVarChar, material.AcceptResult);
			sql.AddParameter("@Num", SqlDbType.Int, material.Num);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertMaterial);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) material.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Material material)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Code", SqlDbType.NVarChar, material.Code);
			sql.AddParameter("@InPosition", SqlDbType.Bit, material.InPosition);
			sql.AddParameter("@UnitCode", SqlDbType.NVarChar, material.UnitCode);
			sql.AddParameter("@BookDate", SqlDbType.DateTime, material.BookDate);
			sql.AddParameter("@PreArriveDate", SqlDbType.DateTime, material.PreArriveDate);
			sql.AddParameter("@ArriveTimeDate", SqlDbType.DateTime, material.ArriveTimeDate);
			sql.AddParameter("@AcceptResult", SqlDbType.NVarChar, material.AcceptResult);
			sql.AddParameter("@Num", SqlDbType.Int, material.Num);
			sql.AddParameter("@Id", SqlDbType.Int, material.Id);
			sql.ExecuteSql(SqlUpdateMaterial);
		}

		public void Delete(Core.Business.Material material)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, material.Id);
			sql.ExecuteSql(SqlDeleteMaterial);
		}

		public IList<Core.Business.Material> GetAllMaterial()
		{
			IList<Core.Business.Material> materiallist = new List<Core.Business.Material>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllMaterial);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Material material = new Core.Business.Material();

					if (!reader.IsDBNull(0)) material.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) material.Code = reader.GetString(1);
					if (!reader.IsDBNull(2)) material.InPosition = reader.GetBoolean(2);
					if (!reader.IsDBNull(3)) material.UnitCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) material.BookDate = reader.GetDateTime(4);
					if (!reader.IsDBNull(5)) material.PreArriveDate = reader.GetDateTime(5);
					if (!reader.IsDBNull(6)) material.ArriveTimeDate = reader.GetDateTime(6);
					if (!reader.IsDBNull(7)) material.AcceptResult = reader.GetString(7);
					if (!reader.IsDBNull(8)) material.Num = reader.GetInt32(8);

					material.MarkOld();
					materiallist.Add(material);
				}
				reader.Close();
			}
			return materiallist;
		}
		#endregion
	}
}
